In [1]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anime
import scipy 
from scipy import stats
In [2]:
# test 2.8.1_Entropy
def H(p):
    h=0
    for k in p:
        h += -1*k*np.log2(k)
        
    return h
In [3]:
H([0.25,0.25,0.2,0.15,0.15])
Out[3]:
2.2854752972273342
In [4]:
def KL(ArrayP,ArrayQ):
    if len(ArrayP)!=len(ArrayQ):
        print "Not mutch len(ArrayP) and len(ArrayQ)"
    
    kl=0
    for p,q in zip(ArrayP,ArrayQ):
        #print p,q
        kl +=p*np.log2(p/q)
    
    return kl
In [5]:
def kl1(p,q):
    p = np.array(p)
    q = np.array(q)
    
    p = p/p.sum()
    q = q/q.sum()
    
    return np.sum(p * np.log2(p / q), axis=(p.ndim - 1))
In [6]:
def kl2(p,q):
    p = np.array(p)
    q = np.array(q)
    
    return np.sum(p * np.log2(p / q), axis=(p.ndim - 1))
In [7]:
def JSD(ArrayP,ArrayQ):
    if len(ArrayP)!=len(ArrayQ):
        print "Not mutch len(ArrayP) and len(ArrayQ)"
    
    jsd=0
    for p,q in zip(ArrayP,ArrayQ):
        m = (p+q)/2
        jsd +=0.5*( p*np.log2(p/m) ) + 0.5*( p*np.log2(p/m) )
    return jsd
In [8]:
test1=np.random.rand(10000)
test2=np.random.rand(10000)
In [9]:
KL(test1,test2)
Out[9]:
3728.690982866779
In [10]:
kl1(test1,test2)
Out[10]:
0.7274887510446566
In [11]:
kl2(test1,test2)
Out[11]:
3728.6909828667885
In [12]:
JSD(test1,test2)
Out[12]:
781.13726326435994
In [13]:
stats.entropy(test1,test2,base=2)
Out[13]:
0.72748875104465682
In [14]:
test1
Out[14]:
array([ 0.74769244,  0.38667907,  0.22266051, ...,  0.69172792,
        0.73755455,  0.43965489])
In [15]:
test1=np.array(test1)
In [16]:
test1
Out[16]:
array([ 0.74769244,  0.38667907,  0.22266051, ...,  0.69172792,
        0.73755455,  0.43965489])
In [17]:
x=np.array(range(0,10000))
In [18]:
fig = plt.figure()
<matplotlib.figure.Figure at 0x7ff051838ed0>
In [19]:
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax1.scatter(np.arange(10000),test1*100)
plt.show()
In [20]:
ax1.scatter(np.arange(10000),test1*100)
plt.show()
In [58]:
from ipython_animated_array import AnimateArray
import numpy as np


initial = np.random.randint(2, size=(10,10))
AnimateArray([initial], cmap='winter').show()

def get_next(current):
    next_state = []
    for i in range(10):
        next_row = []
        for j in range(10):
            living = current[i-1][j-1] + current[i-1][j] + current[i-1][(j+1)%10] + \
                        current[i][j-1] + current[i][j] + current[i][(j+1)%10] + \
                        current[(i+1)%10][j-1] + current[(i+1)%10][j] + current[(i+1)%10][(j+1)%10]
            next_row.append(1 if living in (3,4) else 0)
        next_state.append(next_row)
    return next_state

states = [initial]
current = initial
for i in range(1000):
    current = get_next(current)
    states.append(np.array(current))
test = np.array(states)
print test.shape
AnimateArray(states, cmap='winter').show(reflesh=400)
(1001, 10, 10)
In [32]:
from ipython_animated_array import AnimateArray
import numpy as np
In [33]:
initial = np.random.randint(2, size=(10,10))
In [34]:
initial
Out[34]:
array([[0, 0, 0, 0, 0, 0, 1, 0, 1, 1],
       [1, 1, 1, 0, 0, 0, 1, 1, 1, 0],
       [1, 1, 0, 1, 0, 1, 1, 0, 0, 0],
       [0, 1, 0, 0, 0, 1, 0, 1, 0, 0],
       [1, 0, 1, 1, 1, 0, 0, 0, 0, 1],
       [0, 1, 1, 0, 1, 0, 0, 0, 0, 1],
       [0, 0, 1, 1, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 0, 1, 1, 1],
       [1, 1, 1, 0, 1, 0, 0, 1, 1, 0],
       [0, 0, 1, 1, 0, 1, 1, 1, 1, 0]])
In [36]:
AnimateArray([initial],cmap='spring').show()
In [90]:
states = np.array(initial)
for i in range(1000):
    tmp = np.random.randint(2, size=(10,10))
    states=np.vstack((states,np.array(tmp)))
In [89]:
states.reshape(1001,10,10)
Out[89]:
array([[[1, 0, 0, ..., 0, 0, 1],
        [1, 0, 1, ..., 0, 1, 0],
        [0, 1, 0, ..., 0, 1, 1],
        ..., 
        [0, 1, 0, ..., 1, 0, 0],
        [0, 0, 0, ..., 0, 1, 0],
        [1, 0, 0, ..., 1, 1, 0]],

       [[1, 1, 0, ..., 1, 0, 1],
        [0, 1, 0, ..., 1, 1, 0],
        [1, 1, 1, ..., 1, 1, 0],
        ..., 
        [0, 1, 1, ..., 0, 0, 1],
        [1, 1, 0, ..., 0, 0, 1],
        [0, 0, 1, ..., 1, 0, 0]],

       [[1, 0, 0, ..., 1, 1, 1],
        [1, 1, 1, ..., 0, 1, 1],
        [1, 1, 0, ..., 1, 0, 1],
        ..., 
        [0, 1, 0, ..., 1, 1, 1],
        [1, 1, 1, ..., 0, 1, 0],
        [0, 1, 1, ..., 0, 0, 0]],

       ..., 
       [[1, 0, 0, ..., 1, 1, 1],
        [0, 1, 1, ..., 1, 1, 0],
        [1, 0, 0, ..., 1, 1, 0],
        ..., 
        [0, 1, 0, ..., 0, 0, 1],
        [1, 0, 0, ..., 0, 0, 1],
        [0, 0, 0, ..., 0, 0, 0]],

       [[0, 1, 0, ..., 0, 0, 1],
        [0, 1, 1, ..., 0, 0, 1],
        [1, 1, 1, ..., 1, 1, 0],
        ..., 
        [0, 1, 0, ..., 0, 0, 1],
        [1, 0, 1, ..., 1, 0, 1],
        [0, 1, 0, ..., 1, 1, 0]],

       [[1, 1, 1, ..., 0, 1, 1],
        [0, 0, 1, ..., 1, 0, 1],
        [1, 0, 1, ..., 1, 0, 1],
        ..., 
        [1, 1, 0, ..., 1, 1, 1],
        [0, 0, 1, ..., 0, 0, 0],
        [1, 0, 0, ..., 1, 1, 1]]])
In [63]:
states.shape
Out[63]:
(10010, 10)
In [74]:
AnimateArray(states, cmap='winter').show(reflesh=400)
In [79]:
nstates = states[:,:,newaxis]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-79-ac7179525850> in <module>()
----> 1 nstates = states[:,:,newaxis*10]

TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
In [72]:
nstates.reshape(1001,10,10)
Out[72]:
array([[[1, 0, 0, ..., 0, 0, 1],
        [1, 0, 1, ..., 0, 1, 0],
        [0, 1, 0, ..., 0, 1, 1],
        ..., 
        [0, 1, 0, ..., 1, 0, 0],
        [0, 0, 0, ..., 0, 1, 0],
        [1, 0, 0, ..., 1, 1, 0]],

       [[1, 1, 0, ..., 1, 0, 1],
        [0, 1, 0, ..., 1, 1, 0],
        [1, 1, 1, ..., 1, 1, 0],
        ..., 
        [0, 1, 1, ..., 0, 0, 1],
        [1, 1, 0, ..., 0, 0, 1],
        [0, 0, 1, ..., 1, 0, 0]],

       [[1, 0, 0, ..., 1, 1, 1],
        [1, 1, 1, ..., 0, 1, 1],
        [1, 1, 0, ..., 1, 0, 1],
        ..., 
        [0, 1, 0, ..., 1, 1, 1],
        [1, 1, 1, ..., 0, 1, 0],
        [0, 1, 1, ..., 0, 0, 0]],

       ..., 
       [[1, 0, 0, ..., 1, 1, 1],
        [0, 1, 1, ..., 1, 1, 0],
        [1, 0, 0, ..., 1, 1, 0],
        ..., 
        [0, 1, 0, ..., 0, 0, 1],
        [1, 0, 0, ..., 0, 0, 1],
        [0, 0, 0, ..., 0, 0, 0]],

       [[0, 1, 0, ..., 0, 0, 1],
        [0, 1, 1, ..., 0, 0, 1],
        [1, 1, 1, ..., 1, 1, 0],
        ..., 
        [0, 1, 0, ..., 0, 0, 1],
        [1, 0, 1, ..., 1, 0, 1],
        [0, 1, 0, ..., 1, 1, 0]],

       [[1, 1, 1, ..., 0, 1, 1],
        [0, 0, 1, ..., 1, 0, 1],
        [1, 0, 1, ..., 1, 0, 1],
        ..., 
        [1, 1, 0, ..., 1, 1, 1],
        [0, 0, 1, ..., 0, 0, 0],
        [1, 0, 0, ..., 1, 1, 1]]])
In [83]:
np.reshape(states,states.shape+(1,))
Out[83]:
array([[[1],
        [0],
        [0],
        ..., 
        [0],
        [0],
        [1]],

       [[1],
        [0],
        [1],
        ..., 
        [0],
        [1],
        [0]],

       [[0],
        [1],
        [0],
        ..., 
        [0],
        [1],
        [1]],

       ..., 
       [[1],
        [1],
        [0],
        ..., 
        [1],
        [1],
        [1]],

       [[0],
        [0],
        [1],
        ..., 
        [0],
        [0],
        [0]],

       [[1],
        [0],
        [0],
        ..., 
        [1],
        [1],
        [1]]])
In [91]:
def datarray_to_3D(data, ndim=3):
    (nr,nc) = data.shape
    nparticles = nc/ndim
    dat_3D = np.zeros([nr,ndim,nparticles])
    for i in range(nparticles):
        dat_3D[:,:,i] = data[:,i*ndim:(i+1)*ndim]
    return dat_3D 
In [120]:
states = np.array(initial)
for i in range(1000):
    tmp = np.random.randint(255, size=(10,10))
    states=np.vstack((states,np.array(tmp)))
In [121]:
states.shape
Out[121]:
(10010, 10)
In [122]:
states.reshape(1001,100)
Out[122]:
array([[  1,   0,   0, ...,   1,   1,   0],
       [116,   9,  43, ..., 110, 216,  80],
       [ 79,  23, 210, ..., 244,  70, 195],
       ..., 
       [ 58,  59, 129, ..., 252,  52,  15],
       [ 32, 180,  82, ..., 179, 186, 228],
       [181, 200, 123, ..., 144,  18,  31]])
In [123]:
test = np.reshape(states,states.shape+(1,))
In [124]:
test= test.reshape(1001,10,10)
In [125]:
AnimateArray(test, cmap='winter').show(reflesh=400)
In [ ]:
AnimateArray(test, cmap='winter').save('hoge.mp4')
In [ ]: